package com.myf;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.ZonedDateTime;

public class Utility {

    /// Asia/Shanghai
    public static ZonedDateTime getCurrentUTCTime() {
        return ZonedDateTime.now();
    }

    private static final byte[] key = "whs;POIU1u34+s=r".getBytes(StandardCharsets.UTF_8);
//    public  static  ZonedDateTime getCurrentUTCTime(ZoneId zoneId) {
//        return ZonedDateTime.now(zoneId);
//    }

    public static Integer tryParse(String s) {
        if (s == null || s.isBlank()) {
            return null;
        }
        try {
            return Integer.valueOf(s);
        } catch (NumberFormatException e) {
            return null;
        }
    }

    public static String encrypt(String val) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(val.getBytes(StandardCharsets.UTF_8));
        return java.util.Base64.getEncoder().encodeToString(encrypted);
//        var messageDigest = MessageDigest.getInstance("SHA-256");
//        messageDigest.update(key);
//        return java.util.Base64.getEncoder().encodeToString(messageDigest.digest());
    }

//    public static String decrypt(String val) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
//        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
//        Cipher cipher = Cipher.getInstance("AES");
//        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
//        byte[] encrypted = java.util.Base64.getDecoder().decode(val);
//        byte[] original = cipher.doFinal(encrypted);
//        return new String(original, StandardCharsets.UTF_8);
//    }
}
